﻿using Consul;
using MediatR;
using RedDragon.Common.Domain.InventoryAllocate;
using RedDragon.Common.Infrastructure.Interface;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Inventoryscheduling
{
    public class InventoryschedulingCreateHandler : IRequestHandler<InventoryschedulingCreateCommon, Result>
    {
        public IBaseRepository<Inventory> InventoryRepository;

        public InventoryschedulingCreateHandler(IBaseRepository<Inventory> inventoryRepository)
        {
            InventoryRepository = inventoryRepository;
        }

        public async Task<Result> Handle(InventoryschedulingCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "库存调拨新增失败" };
            try
            {
                var res = new Inventory()
                {
                    AllocateNumber = request.AllocateNumber,
                    AllocateDate = request.AllocateDate,
                    SourcerepositoryId = request.SourcerepositoryId,
                    SourcerepositoryAdd = request.SourcerepositoryAdd,
                    TargetrepositoryId = request.TargetrepositoryId,
                    TargetrepositoryAdd = request.TargetrepositoryAdd,
                    Status = 1,
                    Remark = request.Remark,
                    CreateName = request.CreateName,
                    Markdepartment = request.Markdepartment,
                    CreateTime = DateTime.Now,
                    produceplanAudit = 0
                };
                var res1 = await InventoryRepository.Add(res);
                if (res1 != 0)
                {
                    result.Code = 1;
                    result.Msg = "库存调拨新增成功";
                }



            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
